﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SLGameEngine
{
    public class TerrainMgr
    {
        public ObjectTerrain[,] TerrainTiles;
        public int TerrainTileRadius;
        public int TerrainWidthInTiles;
        public int TerrainHeightInTiles;
        public string MapName = Consts.Unknown;

        private Canvas _mainCanvas = null;

        public TerrainMgr(Canvas mainCanvas)
        {
            _mainCanvas = mainCanvas;
        }

        public void CreateNewMap(int widthInTiles, int heightInTiles, int tileRadius, string mapName, int layerCount)
        {
            _mainCanvas.Children.Clear();

            MapName = mapName;
            TerrainWidthInTiles = widthInTiles;
            TerrainHeightInTiles = heightInTiles;
            TerrainTileRadius = tileRadius;

            TerrainTiles = new ObjectTerrain[TerrainHeightInTiles, TerrainWidthInTiles];

            _mainCanvas.Width = TerrainWidthInTiles * TerrainTileRadius;
            _mainCanvas.Height = TerrainHeightInTiles * TerrainTileRadius;

            for (int y = 0; y < TerrainHeightInTiles; y++)
            {
                for (int x = 0; x < TerrainWidthInTiles; x++)
                {
                    ObjectTerrain terrainObj = new ObjectTerrain(TerrainTileRadius * x, TerrainTileRadius * y, TerrainTileRadius, layerCount, Consts.None);
                    TerrainTiles[y, x] = terrainObj;
                    _mainCanvas.Children.Add(terrainObj);
                }
            }
        }

        public void ShowGrid(bool show)
        {
            Consts.ShowGrid = show;
            for (int y = 0; y < TerrainHeightInTiles; y++)
            {
                for (int x = 0; x < TerrainWidthInTiles; x++)
                {
                    ObjectTerrain tile = TerrainTiles[y, x];
                    tile.Deselect();
                }
            }
        }

        public void ToggleGrid()
        {
            Consts.ShowGrid = !Consts.ShowGrid;
            for (int y = 0; y < TerrainHeightInTiles; y++)
            {
                for (int x = 0; x < TerrainWidthInTiles; x++)
                {
                    ObjectTerrain tile = TerrainTiles[y, x];
                    tile.Deselect();
                }
            }
        }
    }
}
